import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;


public class Enemy {
	
	public static final int DEBUG_SHOOTING_DELAY = 10;
	public static final int ENEMY_WIDTH = 10;
	public static final int ENEMY_HEIGHT = 20;
	
	public static final int GAME_STATUS_DEBUG = 0;
	public static final int GAME_STATUS_NORMAL = 1;
	
	public static int ENEMY_ALIVE 		= 10000;
	public static int ENEMY_DEAD 		= 10001;
	public static int ENEMY_FREEZE 		= 10002;
	public static int ENEMY_CRUSHED		= 10003;
	
	//int gameStatus = GAME_STATUS_DEBUG; 
	int gameStatus = GAME_STATUS_NORMAL;
	
	ArrayList<Bullet> missileList = new ArrayList<Bullet>();
	
	Point enemyPosition;
	Rectangle enemyCollider;
	Point _movementPt;
	
	int shootingDelay = DEBUG_SHOOTING_DELAY;
	int updateCounter ;
	
	boolean fireStatus = false;
	
	int enemyStatus = ENEMY_DEAD;
	
	public Enemy(Point _pt)
	{
		enemyStatus = ENEMY_ALIVE;
		updateCounter = 0;
		enemyPosition = new Point(_pt.x,_pt.y);
		_movementPt= new Point( - 10 ,  0);
		enemyCollider = new Rectangle();
	}
	
	public Enemy(int _x , int _y)
	{
		enemyStatus = ENEMY_ALIVE;
		updateCounter = 0;
		enemyPosition = new Point(_x,_y);
		_movementPt= new Point( - 10 ,  0);
		enemyCollider = new Rectangle();
	}
	public int getEnemyStatus ()
	{
		
		return  enemyStatus;
	}
	public void setEnemyStatus (int _enemyStatus )
	{
		enemyStatus = _enemyStatus;
	}
	public void render(Graphics g)
	{
		g.drawRect( enemyPosition.x , enemyPosition.y, ENEMY_WIDTH, ENEMY_HEIGHT);
		for( int i = 0 ; i < missileList.size(); i++ )
		{
			g.drawOval(((int)missileList.get(i).getMissilePosition().x), 
					((int)missileList.get(i).getMissilePosition().y) ,
					Bullet.BULLET_WIDTH , Bullet.BULLET_HEIGHT);
		}
	}
	
	public void update() {
		if ( gameStatus == GAME_STATUS_NORMAL )
		{
			enemyPosition.x = enemyPosition.x + _movementPt.x;
			enemyPosition.y = enemyPosition.y + _movementPt.y;
			
			if ( enemyPosition.x < 0 ) enemyStatus = ENEMY_DEAD;
			if ( enemyPosition.y < 0 ) enemyStatus = ENEMY_DEAD;
			if ( enemyPosition.x > MainFrame.WINDOW_SIZE_X ) enemyStatus = ENEMY_DEAD;
			if ( enemyPosition.y > MainFrame.WINDOW_SIZE_Y ) enemyStatus = ENEMY_DEAD;
			
		}
		else
		{
			
		}
		
		
		
		  
		if ( ++updateCounter > 10 )
		{
			shootBullet(enemyPosition.x - 10, enemyPosition.y + 2 );
			shootBullet(enemyPosition.x - 10, enemyPosition.y  );
			shootBullet(enemyPosition.x - 10, enemyPosition.y - 2 );
			updateCounter = 0 ;
		}
		
		for (int i = 0 ; i < missileList.size(); i ++ )
		{
			missileList.get(i).update();
			
			if ( missileList.get(i).getBulletStatus() == Bullet.STATUS_DEAD )
			{
				missileList.remove(i--);
			}
		}
	}
	public ArrayList<Bullet> getBulletList()
	{
		
		return  missileList;
	}
	public void shootBullet(Point _destinationPoint)
	{
		missileList.add(new Bullet(enemyPosition,_destinationPoint));
	}
	
	public void shootBullet(int _destX, int _destY)
	{
		missileList.add(new Bullet(enemyPosition,_destX , _destY));
		
	}
	
	public Rectangle getEnemyRect()
	{
		enemyCollider.x = enemyPosition.x;
		enemyCollider.y = enemyPosition.y;
		enemyCollider.width = ENEMY_WIDTH;
		enemyCollider.height = ENEMY_HEIGHT;
		
		return enemyCollider;  
	}
}
